from rest_framework import serializers
# from drf_haystack import serializers

from recruitment.models import City, Recruit, Enterprise
from users.models import User


# 热门城市
class HotCitySerializer(serializers.ModelSerializer):
    class Meta:
        model = City
        fields = '__all__'


# 推荐职位and最新职位
class EnterpriseSerializer(serializers.ModelSerializer):
    class Meta:
        model = Enterprise
        fields = ('id','name','labels','logo','recruits','summary')


class RecruitSerializer(serializers.ModelSerializer):
    enterprise = EnterpriseSerializer()
    class Meta:
        model = Recruit
        exclude = ('users',)


# 搜索职位
from drf_haystack.serializers import HaystackSerializer
from recruitment.search_indexes import RecruitIndex


class EnterprisesSerializer(serializers.ModelSerializer):
    class Meta:
        model = Enterprise
        fields = ('id','name','labels','logo','recruits','summary')

class RecruitsSerializer(serializers.ModelSerializer):
    enterprise = EnterprisesSerializer()
    class Meta:
        model = Recruit
     # 返回除了搜索字段外的所需要的其他字段数据, 可以将所有需要返回的字段数据写上,便于提取
     #    fields = ('id','jobname','salary','condition','education','type','createtime','labels','city','enterprise')
        fields = ('enterprise', )

class RecruitIndexSerializer(HaystackSerializer):
    # object = RecruitsSerializer(read_only=True)
    # enterprise = RecruitsSerializer(read_only=True)
    # enterprise = EnterprisesSerializer()
    class Meta:
        index_classes = [RecruitIndex]
        fields = ('text','id','jobname','salary','condition','education','type','createtime','labels','city','enterprise')





# 职位详情
class UsersSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = '__all__'

class RecruitsDetailsSerializer(serializers.ModelSerializer):
    enterprise = EnterpriseSerializer()
    class Meta:
        model = Recruit
        # fields = '__all__'
        fields = ('id','enterprise','jobname','salary','condition','education','type','city','createtime','labels')

class EnterpriseDetailsSerializer(serializers.ModelSerializer):
    users = UsersSerializer(many=True)
    recruits = RecruitsDetailsSerializer(many=True)
    class Meta:
        model = Enterprise
        # fields = '__all__'
        fields = ('id','recruits','name','summary','content','city','address','labels','coordinate','logo','visits','url','users')

class RecruitDetailsSerializer(serializers.ModelSerializer):
    users = UsersSerializer(many=True)
    enterprise = EnterpriseDetailsSerializer()

    class Meta:
        model = Recruit
        fields = ('id','enterprise','jobname','salary','condition','education','type','city','address','state','labels','detailcontent','detailrequire','visits','createtime','users')
        # fields = '__all__'


# 收藏职位
class RecruitCollectSerializer(serializers.ModelSerializer):
    class Meta:
        model = Recruit
        fields = '__all__'
# 收藏企业
class EnterpriseCollectSerializer(serializers.ModelSerializer):
    class Meta:
        model = Enterprise
        fields = '__all__'


# 企业详情
class RecruitsetSerializer(serializers.ModelSerializer):
    enterprise = EnterpriseSerializer()
    class Meta:
        model = Recruit
        fields = ('id','jobname','salary','condition','education','type','city','createtime','enterprise','labels')

class EnterpriseDetailsSeriaizer(serializers.ModelSerializer):
    recruits = RecruitSerializer(many=True)
    users = serializers.PrimaryKeyRelatedField(read_only=True, many=True)
    class Meta:
        model = Enterprise
        fields = '__all__'